package com.gxa.jjty.admin.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.ScoreService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.Score;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@RestController
@RequestMapping("/score")
@Api(tags = "分值管理")
@Log4j2
public class ScoreController {


    @Autowired
    ScoreService scoreService;


    @PostMapping("/getScore")
    @ApiOperation("查询分值管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "数据名称"),
            @ApiImplicitParam(name = "scoreStatus",value = "状态"),
    })
    public Map selectScore( Integer page, Integer size, String name, Integer scoreStatus
    ) throws IllegalAccessException {
        PageHelper.startPage(page,size);
        Score score = new Score();
        score.setName(name);
        score.setScoreStatus(scoreStatus);
        PageInfo<Score> scorePageInfo = scoreService.selectByPage(page, size, score);
        List<Score> scoreList = scorePageInfo.getList();
        long total = scorePageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("scoreList",scoreList);
        map.put("total",total);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }


    @PostMapping("/getById")
    @ApiOperation("查看分值明细")
    public Map selectByIdScore(@RequestParam("id") Long id) throws BusinessException {

        Score score = scoreService.selectById(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(score);
        return jsonResult.getDataMap();
    }


    @PostMapping("/setScore")
    @ApiOperation("编辑分值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expression",value = "表现"),
            @ApiImplicitParam(name = "unit",value = "单位"),
            @ApiImplicitParam(name = "name",value = "数据名称"),
            @ApiImplicitParam(name = "start",value = "开始的值"),
            @ApiImplicitParam(name = "end",value = "结束的值"),
            @ApiImplicitParam(name = "value",value = "分值"),
            @ApiImplicitParam(name = "scoreStatus",value = "状态"),
            @ApiImplicitParam(name = "sort",value = "排序"),
            @ApiImplicitParam(name = "id",value = "id")
    })
    public  Map updateScore(@RequestBody Score score) throws BusinessException {
        scoreService.updateScore(score);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("更新成功");
        return jsonResult.getDataMap();

    }

    @PostMapping("/deleteScore")
    @ApiOperation("删除分值")
    public Map deleteScore(@RequestParam("id") Long id) throws BusinessException {
        scoreService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("删除成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/putScore")
    @ApiOperation("添加分值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expression",value = "表现"),
            @ApiImplicitParam(name = "unit",value = "单位"),
            @ApiImplicitParam(name = "name",value = "数据名称"),
            @ApiImplicitParam(name = "start",value = "开始的值"),
            @ApiImplicitParam(name = "end",value = "结束的值"),
            @ApiImplicitParam(name = "value",value = "分值"),
            @ApiImplicitParam(name = "scoreStatus",value = "状态"),
            @ApiImplicitParam(name = "sort",value = "排序")
    })
    public Map insertScore(@RequestBody Score score) throws BusinessException {
        System.out.println(score);
        scoreService.insertScore(score);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("添加成功");
        return jsonResult.getDataMap();
    }


}
